﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using Kyoh.Lib.Component.Globalization;

namespace Kyoh.App.Crawler
{
    public partial class InstallPluginDialog : Form
    {
        [TypeConverter(typeof(EnumNameConverter))]
        [EnumName(ResourceSource = typeof(Properties.DisplayName))]
        private enum ValidationStatus
        {
            Invalid,
            Valid,
            NotFound,
            Web,
        }

        public InstallPluginDialog()
        {
            InitializeComponent();
            ValidateTextBox();
        }

        public Uri Uri { get; private set; }

        public override Size GetPreferredSize(Size proposedSize)
        {
            return base.GetPreferredSize(new Size(proposedSize.Width, 95));
        }

        private ValidationStatus ValidateTextBox()
        {
            var conv = new EnumNameConverter(typeof(ValidationStatus));
            try
            {
                Uri = new Uri(inputTextBox.Text);
            }
            catch
            {
                inputStatusPictureBox.Image = Properties.Resources.cross_16;
                inputStatusLabel.Text = conv.ConvertToString(ValidationStatus.Invalid);
                return ValidationStatus.Invalid;
            }

            if (!Uri.IsFile)
            {
                inputStatusPictureBox.Image = Properties.Resources.world_16;
                inputStatusLabel.Text = conv.ConvertToString(ValidationStatus.Web);
                return ValidationStatus.Web;
            }
            if (File.Exists(Uri.AbsolutePath))
            {
                inputStatusPictureBox.Image = Properties.Resources.accept;
                inputStatusLabel.Text = conv.ConvertToString(ValidationStatus.Valid);
                return ValidationStatus.Valid;
            }

            inputStatusPictureBox.Image = Properties.Resources.help_16;
            inputStatusLabel.Text = conv.ConvertToString(ValidationStatus.NotFound);
            return ValidationStatus.NotFound;
        }

        private void InputButtonClick(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                inputTextBox.Text = openFileDialog.FileName;
        }

        private void InputTextBoxTextChanged(object sender, EventArgs e)
        {
            ValidateTextBox();
        }

        private void InputTextBoxValidating(object sender, CancelEventArgs e)
        {
            switch (ValidateTextBox())
            {
                case ValidationStatus.NotFound:
                    e.Cancel = true;
                    MessageBox.Show(Properties.Strings.ErrorFileNotFound);
                    break;
                case ValidationStatus.Invalid:
                    e.Cancel = true;
                    MessageBox.Show(Properties.Strings.ErrorInvalidPathOrUri);
                    break;
            }
        }
    }
}